package com.come4loves.game.dbproxy.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.come4loves.core.dbcenter.Entity;
import com.come4loves.core.dbcenter.annotation.Column;
import com.come4loves.core.utils.Splitable;
import com.come4loves.core.utils.StringUtils;
import com.come4loves.game.module.card.model.CardModel;

public class Shop extends Entity<Long> {

	private static final long serialVersionUID = 7210905448209205150L;

	@Column(pk = true)
	public long actorId;

	/**
	 * 卡牌存储字段
	 */
	@Column
	public String cardStore;
	
	/**
	 * 购买卡牌次数统计  刷新卡牌  次数清零
	 */
	@Column
	public String buyCardCount;

	/**
	 * 箱子存储字段
	 */
	@Column
	public String boxStore;
	
	/**
	 * 购买箱子次数统计
	 */
	@Column
	public String buyBoxCount;
	
	/**
	 * 刷新卡牌的时间
	 */
	@Column
	public int refreshTime;

	
	public List<CardModel> cards;
	
	
	public Map<Integer, Integer> buyBoxCountMap = new HashMap<Integer, Integer>();

	public Map<Integer, Integer> buyCardCountMap = new HashMap<Integer, Integer>();
	@Override
	public Long getPkId() {
		return actorId;
	}

	@Override
	public void setPkId(Long pk) {
		this.actorId = pk;
	}

	@Override
	protected Entity<Long> readData(ResultSet rs, int rowNum) throws SQLException {
		Shop shop = new Shop();
		shop.actorId = rs.getLong("actorId");
		shop.cardStore = rs.getString("cardStore");
		shop.buyCardCount = rs.getString("buyCardCount");
		shop.refreshTime = rs.getInt("refreshTime");
		shop.boxStore = rs.getString("boxStore");
		shop.buyBoxCount = rs.getString("buyBoxCount");
		return shop;
	}

	@Override
	protected void hasReadEvent() {
		this.cards = StringUtils.delimiterString2List(this.cardStore, CardModel.class, Splitable.ELEMENT_SPLIT);
		this.buyCardCountMap = StringUtils.delimiterString2IntMap(this.buyCardCount);
		this.buyBoxCountMap = StringUtils.delimiterString2IntMap(this.buyBoxCount);
	}

	@Override
	protected ArrayList<Object> writeData(Boolean containsPK) {
		ArrayList<Object> values = new ArrayList<Object>();
		if (containsPK) {
			values.add(this.actorId);
		}
		values.add(this.cardStore);
		values.add(this.refreshTime);
		values.add(this.boxStore);
		return values;
	}

	@Override
	protected void beforeWritingEvent() {
		this.buyCardCount = StringUtils.numberMap2String(buyCardCountMap);
		this.buyBoxCount = StringUtils.numberMap2String(buyBoxCountMap);
		this.cardStore = StringUtils.collection2SplitString(cards, Splitable.ELEMENT_DELIMITER);
	}

	@Override
	protected void disposeBlob() {
		cardStore = EMPTY_STRING;
		buyBoxCount = EMPTY_STRING;
		buyCardCount = EMPTY_STRING;
	}

}
